package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.VerifiedBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class VerifiedActivity extends BaseActivity {

    private static final String TAG = "VerifiedActivity";
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.ll_basic_verigied)
    LinearLayout llBasicVerigied;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.tv_review_status)
    TextView tvReviewStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id_card_number)
    TextView tvIdCardNumber;
    @BindView(R.id.ll_advanced_verigied)
    LinearLayout llAdvancedVerigied;
    @BindView(R.id.tv_yuanyin)
    TextView tvYuanyin;
    @BindView(R.id.tishiyuju)
    TextView tishiyuju;
    private String idCard;
    private int tag = 0;
    private Intent intent;
    private VerifiedBean.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verified);
        ButterKnife.bind(this);
        toolbar(toolbar, getString(R.string.shimingrenzheng), R.mipmap.icon_close);

        initData();
    }

    private void initData() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, "正在加载...");
        dialog.show();
        OkHttpUtils.post()
                .url(Constant.query_user_apply)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        dialog.dismiss();
                        e.printStackTrace();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        dialog.dismiss();
                        logUtilssss.e("TAG实名认证" + response + "");
                        postReponse(response);
                    }

                    public void postReponse(String json) {
                        try {

                            VerifiedBean enterBean = new Gson().fromJson(json, new TypeToken<VerifiedBean>() {
                            }.getType());
                            String fanhuistring = "";
                            if (enterBean.getIdentity_auth().size()>=1){
                                List tips_propose = enterBean.getIdentity_auth();
                                for (int i = tips_propose.size() -1; i>= 0; i--) {
                                    fanhuistring = "     "+(i+1)+": "+tips_propose.get(i) + "\n" + fanhuistring;
                                }
                                tishiyuju.setText(getString(R.string.tishi)+":\n" + fanhuistring);
                            }
                            if (enterBean.getCode() == 1011) {
                                VerifiedBean<VerifiedBean.DataBean> ent = new Gson().fromJson(json, new TypeToken<VerifiedBean<VerifiedBean.DataBean>>() {
                                }.getType());


                                data = ent.getData();
                                tag = 1000;
                                tvReviewStatus.setText(getString(R.string.shiming_yes));
                                tvYuanyin.setVisibility(View.GONE);
                                llBasicVerigied.setVisibility(View.GONE);
                                llAdvancedVerigied.setVisibility(View.VISIBLE);
                                tvName.setText(ent.getData().getName());
                                idCard = ent.getData().getIdcard();
                                if (!TextUtils.isEmpty(idCard)) {
                                    tvIdCardNumber.setText(idCard);
                                }
                            } else if (enterBean.getCode() == 1004) {
                                Intent intent = new Intent(VerifiedActivity.this, LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else if (enterBean.getCode() == 1012) {
                                llBasicVerigied.setVisibility(View.VISIBLE);
                                llAdvancedVerigied.setVisibility(View.GONE);
                                tvYuanyin.setVisibility(View.GONE);

                            } else if (enterBean.getCode() == 1013) {
                                VerifiedBean<VerifiedBean.DataBean> ent = new Gson().fromJson(json, new TypeToken<VerifiedBean<VerifiedBean.DataBean>>() {
                                }.getType());
                                tvReviewStatus.setText(getString(R.string.shiming_loging));
                                tag = 0;
                                tvYuanyin.setVisibility(View.GONE);
                                llBasicVerigied.setVisibility(View.GONE);
                                llAdvancedVerigied.setVisibility(View.VISIBLE);
                                tvName.setText(ent.getData().getName());
                                idCard = ent.getData().getIdcard();
                                if (!TextUtils.isEmpty(idCard)) {
                                    tvIdCardNumber.setText(idCard);
                                }
                            } else if (enterBean.getCode() == 1014) {
                                VerifiedBean<VerifiedBean.DataBean> ent = new Gson().fromJson(json, new TypeToken<VerifiedBean<VerifiedBean.DataBean>>() {
                                }.getType());
                                tvReviewStatus.setText(getString(R.string.shenqingbowei));
                                tvYuanyin.setVisibility(View.VISIBLE);
                                tag = 1;
                                llBasicVerigied.setVisibility(View.GONE);
                                llAdvancedVerigied.setVisibility(View.VISIBLE);
                                tvYuanyin.setText(getString(R.string.bohuiyuanyin) + ":" + ent.getData().getInfo());
                                tvName.setText(ent.getData().getName());
                                idCard = ent.getData().getIdcard();
                                if (!TextUtils.isEmpty(idCard)) {
                                    tvIdCardNumber.setText(idCard);
                                }
                            } else {
                                showTips(enterBean.getMsg());
                            }

                        } catch (Exception e) {
                            logUtilssss.toasteero(VerifiedActivity.this);
                        }
                    }
                });
    }

    @OnClick({R.id.ll_basic_verigied, R.id.ll_advanced_verigied})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_basic_verigied:
                intent = new Intent(VerifiedActivity.this, ToVerifiedActivity.class);
                intent.putExtra("type", "2");
                startActivityForResult(intent, 4001);
                break;
            case R.id.ll_advanced_verigied:
                if (tag == 1) {
                    intent = new Intent(VerifiedActivity.this, ToVerifiedActivity.class);
                    intent.putExtra("type", "2");
                    startActivityForResult(intent, 4001);
                } else if (tag == 1000) {//已经实名验证查看个人信息
                    intent = new Intent(VerifiedActivity.this, ToVerifiedActivity.class);
                    intent.putExtra("type", "1");
                    intent.putExtra("bean", (Serializable) data);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        VerifiedActivity.this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {//认证后返回刷新
            //添加新地址
            case 4001:
                if (resultCode == RESULT_OK) {
                    initData();
                    setResult(RESULT_OK);
                }
                break;
            default:
                break;
        }
    }

}
